package cc.langk.web.services;

import cc.langk.web.entity.SysStaff;
import cc.langk.web.mapper.SysStaffMapper;
import cc.langk.web.util.PasswdEncryptoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysStaffService {

    @Autowired
    private SysStaffMapper sysStaffMapper;

    /**
     * 保存用户信息
     * @param sysStaff 用户信息
     * @return
     */
    public boolean savaSysStaff(SysStaff sysStaff) {
        int count = sysStaffMapper.insert(sysStaff);
        if (count>0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 通过用户账号查询用户信息
     * @param account   用户账号
     * @return
     */
    public SysStaff findOneByUserAccount(String account) {
        SysStaff sysStaff = sysStaffMapper.selectByUserAccount(account);
        return sysStaff;
    }

    /**
     * 通过用户ID更新用户信息
     * @param sysStaff
     * @return
     */
    public boolean updateById(SysStaff sysStaff){
        int count = sysStaffMapper.updateByPrimaryKey(sysStaff);
        if (count>0){
            return true;
        }else {
            return false;
        }
    }


    /**
     * 通过用户账号删除用户信息
     * @param account
     * @return
     */
    public boolean deleteByUserAccount(String account) {
        int count = sysStaffMapper.deleteByUserAccount(account);
        if (count>0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 查询所有的用户信息
     * @return
     */
    public List<SysStaff> selectAllUser() {
        return sysStaffMapper.selectAll();
    }

    public SysStaff login(String name, String passwd) {
        String newPass = PasswdEncryptoUtil.passwdEncrypto(passwd);
        return sysStaffMapper.selectByUserAccountAndPasswd(name,newPass);
    }

    public SysStaff findOneById(long uid) {
        return sysStaffMapper.selectByPrimaryKey(uid);
    }
}
